package cn.com.miao.lynx.plugin.push;

import cn.com.miao.lynx.plugin.push.aop.advisor.MessagePushClientSwitchAdvisor;
import cn.com.miao.lynx.plugin.push.aop.interceptor.MessagePushSwitchAnnotationInterceptor;
import cn.com.miao.lynx.plugin.push.model.PushClientConfig;
import cn.com.miao.lynx.plugin.push.properties.MessagePushProperties;
import cn.com.miao.lynx.plugin.push.service.MessagePushService;
import cn.com.miao.lynx.plugin.push.service.impl.MessagePushJPushServiceImpl;
import cn.jpush.api.JPushClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * MessagePushAutoConfig
 *
 * @title: MessagePushAutoConfig
 * @description:
 * @author: dengmiao
 * @create: 2020-01-14 13:59
 **/
@Slf4j
@AllArgsConstructor
@Configuration
@ConditionalOnClass({MessagePushService.class, JPushClient.class})
@EnableConfigurationProperties(MessagePushProperties.class)
public class MessagePushAutoConfig {

    /**
     * default client name
     */
    private static final String DEFAULT_CLIENT_NAME = "default";
    /**
     * ApiBoot Message Push Properties
     */
    private final MessagePushProperties messagePushProperties;

    /**
     * instantiation message push service
     *
     * @return ApiBootMessagePushService
     */
    @Bean
    @ConditionalOnMissingBean
    public MessagePushService apiBootMessagePushService() {
        Map<String, PushClientConfig> configs = getClientConfig();
        return new MessagePushJPushServiceImpl(configs, messagePushProperties.isProduction());
    }

    /**
     * Message Push Aop Interceptor
     * @return MessagePushSwitchAnnotationInterceptor
     */
    @Bean
    @ConditionalOnMissingBean
    public MessagePushSwitchAnnotationInterceptor messagePushSwitchAnnotationInterceptor() {
        return new MessagePushSwitchAnnotationInterceptor();
    }

    /**
     * Message Push Aop Advisor
     * @return MessagePushClientSwitchAdvisor
     */
    @Bean
    @ConditionalOnMissingBean
    public MessagePushClientSwitchAdvisor messagePushClientSwitchAdvisor() {
        return new MessagePushClientSwitchAdvisor(messagePushSwitchAnnotationInterceptor());
    }

    /**
     * get client config
     * @return
     */
    private Map<String, PushClientConfig> getClientConfig() {

        Map<String, PushClientConfig> configs = new HashMap(1);

        // default client config
        configs.put(DEFAULT_CLIENT_NAME, messagePushProperties.getClient());

        // multiple
        if (!ObjectUtils.isEmpty(messagePushProperties.getMultiple())) {
            configs.putAll(messagePushProperties.getMultiple());
        }
        return configs;
    }
}
